package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EmployeeAwardsInfoMapper;
import com.ruoyi.business.domain.EmployeeAwardsInfo;
import com.ruoyi.business.service.IEmployeeAwardsInfoService;

import javax.validation.Validator;

/**
 * 员工彰奖励信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-17
 */
@Service
public class EmployeeAwardsInfoServiceImpl  extends BaseServiceImpl implements IEmployeeAwardsInfoService
{
    @Autowired
    private EmployeeAwardsInfoMapper employeeAwardsInfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询员工彰奖励信息
     * 
     * @param id 员工彰奖励信息主键
     * @return 员工彰奖励信息
     */
    @Override
    public EmployeeAwardsInfo selectEmployeeAwardsInfoById(Long id)
    {
        return employeeAwardsInfoMapper.selectEmployeeAwardsInfoById(id);
    }

    /**
     * 查询员工彰奖励信息列表
     * 
     * @param employeeAwardsInfo 员工彰奖励信息
     * @return 员工彰奖励信息
     */
    @Override
    public List<EmployeeAwardsInfo> selectEmployeeAwardsInfoList(EmployeeAwardsInfo employeeAwardsInfo)
    {
        return employeeAwardsInfoMapper.selectEmployeeAwardsInfoList(employeeAwardsInfo);
    }

    /**
     * 新增员工彰奖励信息
     * 
     * @param employeeAwardsInfo 员工彰奖励信息
     * @return 结果
     */
    @Override
    public int insertEmployeeAwardsInfo(EmployeeAwardsInfo employeeAwardsInfo)
    {
        employeeAwardsInfo.setCreateTime(DateUtils.getNowDate());
        return employeeAwardsInfoMapper.insertEmployeeAwardsInfo(employeeAwardsInfo);
    }

    /**
     * 修改员工彰奖励信息
     * 
     * @param employeeAwardsInfo 员工彰奖励信息
     * @return 结果
     */
    @Override
    public int updateEmployeeAwardsInfo(EmployeeAwardsInfo employeeAwardsInfo)
    {
        employeeAwardsInfo.setUpdateTime(DateUtils.getNowDate());
        return employeeAwardsInfoMapper.updateEmployeeAwardsInfo(employeeAwardsInfo);
    }

    /**
     * 批量删除员工彰奖励信息
     * 
     * @param ids 需要删除的员工彰奖励信息主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeAwardsInfoByIds(Long[] ids)
    {
        return employeeAwardsInfoMapper.deleteEmployeeAwardsInfoByIds(ids);
    }

    /**
     * 删除员工彰奖励信息信息
     * 
     * @param id 员工彰奖励信息主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeAwardsInfoById(Long id)
    {
        return employeeAwardsInfoMapper.deleteEmployeeAwardsInfoById(id);
    }

    @Override
    public String importData(List<EmployeeAwardsInfo> userList, boolean updateSupport, String operName) {
    if (userList == null || userList.isEmpty()) {
        throw new ServiceException("导入员工奖励信息数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();
    int i = 1;
    for (EmployeeAwardsInfo user : userList) {
        try {
            i++;
            if (user.getAwardYear() == null || user.getAwardYear().equals("") || user.getEmployeeId() ==null || user.getEmployeeId().equals("") || !isAwardYearValid(user.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }
            // 验证是否存在这个员工奖励信息
            EmployeeAwardsInfo employeeAwardsInfo = new EmployeeAwardsInfo();

            employeeAwardsInfo.setEmployeeId(user.getEmployeeId());
            employeeAwardsInfo.setStartDate(user.getStartDate());
            employeeAwardsInfo.setEndDate(user.getEndDate());
            employeeAwardsInfo.setHonorTitle(user.getHonorTitle());

            List<EmployeeAwardsInfo> selectEmployeeAwardsInfoList = employeeAwardsInfoMapper.selectEmployeeAwardsInfoList(employeeAwardsInfo);


            if (true) {
                // 验证数据
                BeanValidators.validateWithException(validator, user);
                // 设置初始值
                user.setCreateTime(DateUtils.getNowDate());
                user.setCreateBy(operName);
                employeeAwardsInfoMapper.insertEmployeeAwardsInfo(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工奖励信息 "  + " 导入成功");
            } else if (true) {
                // 验证数据
                EmployeeAwardsInfo existingAward = selectEmployeeAwardsInfoList.get(0);
                BeanValidators.validateWithException(validator, user);
                user.setId(existingAward.getId());
                user.setUpdateTime(DateUtils.getNowDate());
                user.setUpdateBy(operName);
                employeeAwardsInfoMapper.updateEmployeeAwardsInfo(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工奖励信息 " + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、员工奖励信息 "  + " 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、员工奖励信息 " + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }
        setCacheInfo("个人荣誉信息",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
